package zhaomh.catgirl.commands;

import org.bukkit.command.*;
import org.bukkit.entity.Player;
import zhaomh.catgirl.zNeko;
import zhaomh.catgirl.zTools;
import zhaomh.catgirl.zConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NekoCommand implements CommandExecutor, TabExecutor {

    zTools t = new zTools("&d[猫娘ねこむすめ]", null);

    public void helpCommand(){
        List<String> list = zConfig.getLanguageList("command.neko.help");
        for (String s : list) {
            t.sendM(s);
        }
    }
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof ConsoleCommandSender) {
            sender.sendMessage(zConfig.getLanguage("command.only-player"));
            return true;
        }

        t.setPlayer((Player) sender);

        if (args.length == 0) {
            helpCommand();
            
        }else if (args.length == 1) {

            switch (args[0]) {
                case "help" -> helpCommand();
                case "catgirl" -> t.sendM2(zConfig.getLanguage("command.neko.catgirl.confirm"));
                case "owner" -> t.sendM2(zConfig.getLanguage("command.neko.owner.y-no"));
                default -> t.sendM2(zConfig.getLanguage("command.no"));
            }
        } else if (args.length == 2) {
            if (args[0].equals("catgirl")) {
                if (args[1].equals("y")) {

                    zNeko.addNeko(t.getPlayer(), true);

                } else {
                    t.sendM2(zConfig.getLanguage("command.neko.catgirl.y-no"));
                }
            } else if (args[0].equals("owner")) {

                List<String> l = t.getPlayerNames();
                l.remove(t.getName());

                if (args[1].equals(t.getName())) {

                    t.sendM2(zConfig.getLanguage("command.neko.owner.me"));

                } else if (l.contains(args[1])) {

                    List<String> list = t.getKeysAtPath(zConfig.data,"catgirls");

                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).equals((args[1]))) {
                            if (zConfig.data.getString("catgirls."+list.get(i)+".owner") != null && zConfig.data.getString("catgirls."+list.get(i)+".owner").equals(t.getName())) {

                                t.sendM2(zConfig.getLanguage("command.neko.owner.both"));
                                return true;
                            }
                        }
                    }

                    String str = zConfig.getLanguage("command.neko.owner.confirm");
                    str = str.replace("${owner}",args[1]);
                    t.sendM2(str);

                } else {
                    t.sendM2(zConfig.getLanguage("command.neko.owner.no-online"));
                }

            } else {
                t.sendM2(zConfig.getLanguage("command.no"));
            }
        } else if (args.length == 3) {
            if (args [0].equals("owner")) {

                List<String> l = t.getPlayerNames();
                l.remove(t.getName());

                if (args[1].equals(t.getName())) {

                    t.sendM2(zConfig.getLanguage("command.neko.owner.me"));

                } else if (l.contains(args[1])) {

                    if (args[2].equals("y")) {

                        zNeko.NewOwner(t.getPlayer(), t.getNamePlayer(args[1]), true);

                    }

                } else {
                    t.sendM2(zConfig.getLanguage("command.neko.owner.no-online"));
                }
            }
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
        List<String> l = new ArrayList<>();;

        if (args.length == 1) {
            l.add("help");
            l.add("catgirl");
            l.add("owner");
        } else if (args.length == 2) {
            if (args[1].equals("owner")) {

                l = t.getPlayerNames();
                l.remove(sender.getName());

            }
        }

        return l;
    }
}